package com.xjrsoft.module.language.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.language.dto.LgMapListDto;
import com.xjrsoft.module.language.entity.XjrLgMap;
import com.xjrsoft.module.language.entity.XjrLgMark;
import com.xjrsoft.module.language.mapper.XjrLgMapMapper;
import com.xjrsoft.module.language.service.IXjrLgMapService;
import com.xjrsoft.module.language.service.IXjrLgMarkService;
import com.xjrsoft.module.language.vo.LgMapMarkVo;
import com.xjrsoft.module.language.vo.LgMapNameVo;
import com.xjrsoft.module.language.vo.LgMapVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 语言映照表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-11-11
 */
@Service
@AllArgsConstructor
public class XjrLgMapServiceImpl extends ServiceImpl<XjrLgMapMapper, XjrLgMap> implements IXjrLgMapService {

    private final IXjrLgMarkService lgMarkService;

    @Override
    public PageOutput<Map<String, Object>> getPageList(LgMapListDto lgMapListDto) {
        String keyword = lgMapListDto.getKeyword();
        if (!StringUtil.isEmpty(keyword)) {
            keyword = StringPool.PERCENT + keyword + StringPool.PERCENT;
        }
        IPage<LgMapMarkVo> page = ConventPage.getPage(lgMapListDto);
        List<LgMapMarkVo> lgMapListVoMark = this.baseMapper.selectTreeList(keyword, page);
        Map<String, Map<String, Object>> resultMap = new HashMap<>(16);
        if (CollectionUtil.isNotEmpty(lgMapListVoMark)) {
            for (LgMapMarkVo lgMapMarkVo : lgMapListVoMark) {
                String id = lgMapMarkVo.getId();
                Map<String, Object> map = resultMap.get(id);
                if (map == null) {
                    map = new HashMap<>(16);
                    map.put("id", lgMapMarkVo.getId());
                    map.put("mkEnCode", lgMapMarkVo.getMkEnCode());
                    map.put("mkName", lgMapMarkVo.getMkName());
                    resultMap.put(id, map);
                }
                List<LgMapVo> lgMapVoList = lgMapMarkVo.getLgMapVoList();
                if (CollectionUtil.isNotEmpty(lgMapVoList)) {
                    for (LgMapVo lgMapVo : lgMapVoList) {
                        map.put(lgMapVo.getTypeCode(), lgMapVo.getName());
                    }
                }
            }
        }
        return ConventPage.getPageOutput(page.getTotal(), new ArrayList<>(resultMap.values()));
    }

    @Override
    @Transactional
    public boolean updateLgMap(String id, Map<String, String> mapDto) {
        List<XjrLgMap> lgMapList = new ArrayList<>();
        List<XjrLgMap> oldMapList = this.list(Wrappers.<XjrLgMap>query().lambda().eq(XjrLgMap::getCode, id));
        // 构建保存数据
        for (Map.Entry<String, String> entry : mapDto.entrySet()) {
            XjrLgMap lgMap = new XjrLgMap();
            String name = entry.getValue();
            lgMap.setName(name);
            for (XjrLgMap oldMap : oldMapList) {
                if (StringUtil.equalsIgnoreCase(entry.getKey(), oldMap.getTypeCode())) {
                    lgMap.setId(oldMap.getId());
                }
            }
            if (StringUtil.isEmpty(lgMap.getId())) {
                lgMap.setCode(id);
                lgMap.setTypeCode(entry.getKey());
            }
            lgMapList.add(lgMap);
        }
        return this.saveOrUpdateBatch(lgMapList);
    }

    @Override
    public List<LgMapNameVo> selectNamesTreeByCode(String typeCode) {
        return this.baseMapper.selectNamesTreeByCode(typeCode);
    }

    @Override
    @Transactional
    public boolean saveLgMapWithMark(XjrLgMark lgMark, Map<String, String> lgMapList) {
        if (StringUtil.isEmpty(lgMark.getParentId())) {
            lgMark.setParentId(StringPool.ZERO);
        }
        String lgMarkId = IdWorker.get32UUID();
        lgMark.setId(lgMarkId);
        List<XjrLgMap> savedMapList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(lgMapList)) {
            lgMapList.forEach((key, value) -> {
                XjrLgMap lgMap = new XjrLgMap();
                lgMap.setCode(lgMarkId);
                lgMap.setTypeCode(key);
                lgMap.setName(value);
                savedMapList.add(lgMap);
            });
        }
        return lgMarkService.save(lgMark) && this.saveBatch(savedMapList);
    }
}
